package com.yy.permissions_general.utils;


import com.yy.permissions_general.module.system.user.entity.User;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Description:获取登录信息
 * @Author: YY
 * @Date: 2021/11/3
 */
public class SecurityUtil {

    /**
     * 获取系统用户
     *
     * @return 系统用户名称
     */
    public static User getCurrentUser() {
        return null != getAuthentication()?(User) getAuthentication().getPrincipal():null;
    }
    /**
     * 获取当前登录用户的信息
     *
     * @return Authentication 权鉴对象
     */
    public static Authentication getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return !(authentication instanceof AnonymousAuthenticationToken)?authentication:null;
    }
    /**
     * 验证当前用户是否登录
     *
     * @return boolean 是否登录
     */
    public static boolean isAuthentication() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        return !(auth instanceof AnonymousAuthenticationToken);
    }
}
